﻿/*
* ==============================================================================
*
* File name: FactoryMethod
* Description: 
*
* Version: 1.0
* Created: 2015-07-07 16:10:56
*
* Author: Your name
* Company: Your company name
*
* ==============================================================================
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _02_工厂方法模式
{
    public abstract class Operation
    {
        double numberA;

        public double NumberA
        {
            get { return numberA; }
            set { numberA = value; }
        }
        double numberB;

        public double NumberB
        {
            get { return numberB; }
            set { numberB = value; }
        }

        //public virtual double GetResult()  //虚方法，派生类中可不重写
        //{
        //    double result = 0;
        //    return result;
        //}
        public abstract double GetResult(); //抽象方法，派生类中必须实现此方法      

    }

    class OperationAdd : Operation
    {
        public override double GetResult()
        {
            //double result = 0;
            //result = NumberA + NumberB;
            //return result;
            return NumberA +NumberB;
        }

       
    }
    /// <summary>
    /// 减法类
    /// </summary>
    class OperationSub : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA - NumberB;
            return result;
        }
    }
    /// <summary>
    /// 乘法类
    /// </summary>
    class OperationMul : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA * NumberB;
            return result;
        }
    }
    /// <summary>
    /// 除法类
    /// </summary>
    class OperationDiv : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            if (NumberB == 0)
                throw new Exception("除数不能为0。");
            result = NumberA / NumberB;
            return result;
        }
    }

    /// <summary>
    /// 工厂用于实例化 对象。
    /// </summary>
    interface IFactory
    {
        Operation CreateOperation();
    }

    /// <summary>
    /// 专门负责生产“+”的工厂
    /// </summary>
    class AddFactory : IFactory
    {
        public  Operation CreateOperation()
        {
            return new OperationAdd();
        }
    }

    /// <summary>
    /// 专门负责生产“-”的工厂
    /// </summary>
    class SubFactory : IFactory
    {
        public Operation CreateOperation()
        {
            return new OperationSub();
        }
    }

    /// <summary>
    /// 专门负责生产“*”的工厂
    /// </summary>
    class MulFactory : IFactory
    {
        public Operation CreateOperation()
        {
            return new OperationMul();
        }
    }

    /// <summary>
    /// 专门负责生产“/”的工厂
    /// </summary>
    class DivFactory : IFactory
    {
        public Operation CreateOperation()
        {
            return new OperationDiv();
        }
    }

}
